package com.lyn.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.lyn.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private  String app_id = "2021000117619201";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDxHvuuGpag8cBa0pirufzE6+nN/0l44zoh+Kt5RbKbozgUNoQT1YDV8M6/JeTEQmUzFWF7DH6HW+nTgKfLJuY9CCygf/j1Uo9khqmqEmoOY08HtwftPkDiAWo5MjVpQfo5eU6M/cR1kWEGCoCSdHB58fnDWDGtjf06DgegpHazm88PZHxyMzliXWsRtQtEP+ZsWkw17Fx3fN8ml3Z940GPYko5wtTFDtK4LMgoP5vsAhQP/o8/A+Y0Wzw1U9+FiHDJ7LvjR7K0QnBQ6lYH6muSW4pZm7ykn2hXZTFPySiY9v1sn4wmPGHSL5Dsz4yv0oyLms/m3mBcAUtGG+lqwy1FAgMBAAECggEAee9HK3Z6sg1PTx6s4jr93r/GzsvI5f9TCJbucDeU6SwTUfV3uWgA7TAH/ZY2KCHDd5/aWhWpbt5GUxT+xbd4O2kE2G2fAsCrjxSifEUvqPMDpk0jDxCqUyCQDTic1gvtdF8qIBa84Q+R1zI8btO/hYue2PCivrfNVSx/IvfIp26qbAw+M4aqwuiI0YiD99pPYBBgPaZaX6x8SGIrku6TbDHFqHb9v9beN2vsw1Y0MdeV52BCT84oSP32rdVa/7N+Mb9NfUhd9EzJrBXFGgx4Xy9jdZoSmQ6eD/u5jwEX3H8cIidPrBZrIEB0roqukJoMYLz2/yzgERWOZh/SD7NBSQKBgQD7bRdFxrCgGDTKkx78FzUnv3mDZIc9KDIti5UPF+EgBRkjhBd7Mbl5+43OVqChg5EAsWo4j/g0NhDkB/dUF78/a4sqXRmfTO8c3EF4BjOKOt17vPzzykFUWYC3fZ5FqXj36mIGju2EQnICdZSr8zbYQ11JO5am/bhn0DUOUgfedwKBgQD1geaOIG85U2cGt8RRA+5PTYvUb2ZD7EJWHJrWGFy0MEHQcUHxItTUI3opBS5kTuwUeW2QmbEmX/CHRC3OVDL4ggphzHUFojm+QteLP+IWQZKVWPVrUWfVQAOzgC0mlqOyKzjjei/F+0wgB+lokSlsPSswxfn6DV4SmNBoURAVIwKBgQDFfniUZ7Ef4EO/h/cDv9sSINZ7PkD46hUYL5djprdApqCLhFvJelKeHdnS5bsBg84nxDSSv4wQvxNfe15s4MZqM7i6Cy1wM0dz3RNUC1L61qhZZScw/IN6lV+FQphLUgbifmqHikfJ50OU6PViaAaBs9CYEMWS4KO0cBjs6cSimwKBgBYVTC7GnhqS9PZ2uzLqXppFiGlgZ9t+Oe1S/154rIYMvS7JsTvGfUxjsI5WB/k/3B5vWbqjCHtCL/mK2W/wxPDziYmIgZIzKGyDyK2e3nBQD6ZdbR3tIaDd9DdGNUl+TVUubBySFhAB7bQH4ifkvswcqHDnrfefEjIsCrsHZ6HvAoGBAJjpSGu/amJsh608ZxK58Jh/K6RGO8rm16+lz1gWTigUR0j54b5FKsaNYBbNUgkL2CLyhyO0v5Vkwfvx4ibqqVyYYKDrSR0I/1NlrzsVPcQYHfptGk/wU/1Gy3kSpWmSQKmnXi1qwVeJCCDXCvL2PA/tLbYpe/C6nDEfBIs+zqxu";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvcSiRF4nUXwaW/A+9GTB1o25OXumdBkOgTMgTt5wdWkLX2ZSxaVLqiI5rxKZ6XludJGauMYbEadUVyKwRt1OCPdWUtKkOkqOZSRRz4pmc+omvSyss5KdEc5BGnPf0i3rDIEaGazENhQxkfcrXfjtFUnNMiXCtr+qL+w1amApAwBk9Ma3w4bK0RucqyiTL6AVIKvGiFajv85BHHkF4aE/iGT3UX8X8TCRJIcBZj/UJKH0JTLI5OfyREVCJN7PKXAX12pA5S1HcLlgBiFpX9PYraQm63yQ6zNP2bVwH5U1qnVUDLiUuGIajrOGe7txIs+qHCcL2B8Y8an1/ScI/bLbfQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://yu34s97h20.52http.net/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url= "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout="30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
